package cleanstarxchange.server;


import java.io.*;
import java.net.*;
import java.util.*;
import java.io.IOException;


public class TweetCounter{
	public static void main(String[] args){
		getCount("jessica alba");	
		
	}
	
	/**
	 * Get the the number of tweets count for given celebrity name that day
	 * @param name - name of the celebrity
	 * @return - the total count of tweets
	 */
	@SuppressWarnings("deprecation")
	public static int getCount(String name){
		//Get today's date
		Calendar cal = Calendar.getInstance();
		Date today = cal.getTime();
		
		//Split the names with delimiter " " 
		String[] nameSplit= new String[2];
		nameSplit = name.split(" ");
		
		String res=null;
		int count=100;
		int max =0;
		int total=0;
		int loop=1;
		int lastMax=0;
		
		int month=today.getMonth()+1;
		int date=today.getDate();
		String dateToMatch=null;
		
		//Retreve date from today object and put into XX_XX format
		if(date <10){
			dateToMatch = Integer.toString(month) + "-0" + Integer.toString(date);
		}
		else{
			dateToMatch = Integer.toString(month) + "-" + Integer.toString(date);
		}
				
		System.out.print(dateToMatch+ ":  ");
		System.out.println();

		//repeat determines how many times to check twitter response
		//they tend to vary depending on the status of twitter server throughput
		int repeat =1;
		int i;
		
		//since the maximum number of tweets you can get is 100 per page
		//loop through the same name until there is no tweets on that page
		while(count > 0){
			System.out.println("Loop: "+ loop);
			max =0;
			for(i=0; i < repeat;i++){
				String addr = "http://search.twitter.com/search.atom?lang=en&q="+nameSplit[0]+"+"+nameSplit[1]+"&rpp=100&page=" + Integer.toString(loop);
				count =0;
				
				try{
					//Setting HTTP connection with method GET
					URL url = new URL(addr);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod("GET");
					
					conn.connect();
					InputStream in = conn.getInputStream();
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					//reading in all the response from twitter server
					while(reader.ready()){
						res = reader.readLine();
						
						if(res.contains("published")&&res.contains(dateToMatch)){
							//System.out.println(res);
							count++;
						}
					}
					conn.disconnect();
					
					
				}
				catch(IOException ex){
					//when IO exception encounter spot looping and return current total
					ex.printStackTrace();
					System.out.println("error here");
					total = (loop-3)*100 + lastMax;
					return total;
				}
				
				if(count > max){
					max = count;
				}
				System.out.print(count + " ");
				if(max > 0)
					lastMax = max;
			}
		
			System.out.println();
			System.out.println("Max: " + max);
			//total += max;
			System.out.println();
			loop++;
		}
			
		System.out.println();
		//total is 100* number of pages + last
		total = (loop-3)*100 + lastMax;
		

		
		System.out.println("Total: " + total);
		return total;
	}
	
}




